﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessTier;
using DataTier;

namespace GUI
{
    
    public partial class EmployeeForm : Form
    {
        private static EmployeeForm instance = null;
        private EmpCtr _empCtr = EmpCtr.Instance;
        private MainForm _mainform = MainForm.Instance;

        public static EmployeeForm Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new EmployeeForm();
                }
                return instance;
            }
        }

        public EmployeeForm()
        {
            InitializeComponent();
        }

        private void btnSearchEmp_Click(object sender, EventArgs e)
        {
            SearchEmployee();
            
        }

        private void buildDatagrid()
        {
            dgvEmployee.AutoGenerateColumns = false;

            DataGridViewTextBoxColumn firstNameColumn = new DataGridViewTextBoxColumn();
            firstNameColumn.DataPropertyName = "_firstName";
            firstNameColumn.HeaderText = "First Name";

            DataGridViewTextBoxColumn lastNameColumn = new DataGridViewTextBoxColumn();
            lastNameColumn.DataPropertyName = "_lastName";
            lastNameColumn.HeaderText = "Last Name";

            DataGridViewTextBoxColumn empNrColumn = new DataGridViewTextBoxColumn();
            empNrColumn.DataPropertyName = "_empNr";
            empNrColumn.HeaderText = "Employee Number";

            dgvEmployee.Columns.Add(firstNameColumn);
            dgvEmployee.Columns.Add(lastNameColumn);
            dgvEmployee.Columns.Add(empNrColumn);
        }

        private void btnSelectEmp_Click(object sender, EventArgs e)
        {
            if (dgvEmployee.SelectedRows != null && dgvEmployee.RowCount > 0)
            {
                Employee emp = (Employee)dgvEmployee.SelectedRows[0].DataBoundItem;
                _mainform.showEmployeeInTxt(emp);
            }
           this.Close();
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            instance = null;
        }

        private void SearchEmployee()
        {
            string firstname = txtFirstName.Text;
            string lastname = txtLastName.Text;
            List<Employee> emps = _empCtr.GetAllEmployees(firstname, lastname);

            dgvEmployee.DataSource = null;
            dgvEmployee.DataSource = emps;
        }

        private void dgvEmployee_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvEmployee.SelectedRows != null && dgvEmployee.RowCount > 0)
            {
                Employee emp = (Employee)dgvEmployee.SelectedRows[0].DataBoundItem;
                _mainform.showEmployeeInTxt(emp);
            }
            this.Close();
        }

        private void txtFirstName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Return))
            {
                SearchEmployee();
            }
        }

        private void txtLastName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Return))
            {
                SearchEmployee();
            }
        }

       
    }
}